/*
 * ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is Topless JSF.
 *
 * The Initial Developer of the Original Code is
 * Marco Antonio Villalobos, Jr. (mvillalobos@kineteque.com).
 * Portions created by the Initial Developer are Copyright (C) 2010
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * Marco Antonio Villalobos, Jr. (mvillalobos@kineteque.com).
 *
 * ***** END LICENSE BLOCK *****
 */

package kineteque.jsf.topless.util;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;

/**
 * Provides JSON conversion utilities.
 */
public class JSONConverter {

    /**
     * Converts the given json text into a List.
     *
     * @param json The json to convert.
     * @return A List object.
     * @throws JSONException When there is an error parsing this.
     */
    public static List jsonArrayToList(String json) throws JSONException {
        JSONArray a = new JSONArray(json);
        return jsonArrayToList(a);
    }

    /**
     * Converts the given json object into a List.
     *
     * @param a JSON Array to convert.
     * @return A List object.
     * @throws JSONException When there is a JSON error.
     */
    public static List jsonArrayToList(JSONArray a) throws JSONException {
        int length = a.length();
        List l = new ArrayList(length);
        for (int i=0;i<length;i++) {
            Object v = a.get(i);
            if (v instanceof JSONObject){
                l.add(jsonObjectToMap((JSONObject)v));
            } else if ((v instanceof JSONArray)){
                l.add(jsonArrayToList((JSONArray)v));
            } else {
                l.add(v);
            }
        }
        return l;
    }

    /**
     * Converts the given json text into a map.
     * @param json The json to convert.
     * @return A Map object.
     * @throws JSONException When there is an error parsing this.
     */
    public static Map jsonObjectToMap(String json) throws JSONException {
        JSONObject j = new JSONObject(json);
        return jsonObjectToMap(j);
    }

    /**
     * Converts the given json object into a map.
     * @param j The json to convert.
     * @return A Map object.
     * @throws JSONException When there is an error parsing this.
     */
    public static Map jsonObjectToMap(JSONObject j) throws JSONException {
        Map map =  new HashMap();
        for (String k:JSONObject.getNames(j)) {
            Object v = j.get(k);
            if (v instanceof JSONObject) {
                map.put(k,jsonObjectToMap((JSONObject)v));
            } else if ((v instanceof JSONArray)){
                map.put(k,jsonArrayToList((JSONArray)v));
            } else {
                map.put(k,v);
            }
        }
        return map;
    }

    /**
     * Converts the list into a JSON String.
     * @param list The list to convert.
     * @return The json string.
     */
    public static String listToJsonArray(List list) {
        JSONArray jsonArray = new JSONArray(list);
        return jsonArray.toString();
    }
}
